package com.czkt.springboot_mybatis_test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czkt.springboot_mybatis_test.entity.Page;
import com.czkt.springboot_mybatis_test.entity.Product;
import com.czkt.springboot_mybatis_test.entity.SysUser;
import com.czkt.springboot_mybatis_test.entity.vo.StorageProduct;
import com.czkt.springboot_mybatis_test.service.IProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/springboot_mybatis_test/product")
public class ProductController {
    @Resource
    IProductService productService;

    @RequestMapping("/crm/product/list")
    public String productShow(String prodName, String prodType, String prodBatch, Integer pageIndex, Model model){
        if(pageIndex == null){
            pageIndex = 1;
        }
        Page page = new Page();

        if(prodName == null){
            prodName = "";
        }
        if(prodType == null){
            prodType = "";
        }
        if(prodBatch == null){
            prodBatch = "";
        }
        Integer count = productService.selectProductCount(prodName, prodType, prodBatch);
        if(count == 0){
            count = 1;
        }
        page.setTotalCount(count);
        if(pageIndex < 1){
            pageIndex = 1;
        }
        if(pageIndex > page.getTotalPageCount()){
            pageIndex = page.getTotalPageCount();
        }
        int index = (pageIndex - 1) * page.getPageSize();
        page.setPageNo(pageIndex);

        List<Product> products = productService.selectProduct(prodName,prodType,prodBatch,index,page.getPageSize());
        page.setProductList(products);
        model.addAttribute("productPage",page);
        model.addAttribute("prodName",prodName);
        model.addAttribute("prodType",prodType);
        model.addAttribute("prodBatch",prodBatch);
        return "/product/list";
    }
}
